<?php

namespace app\store\controller;

use app\admin\common\Base;
use think\Db;

//商品收发明细表
class Goodsflowdetail extends Base
{

    public function index()
    {
        $data = $this->request->param();

        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);

            $list = Db::name('goods')->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->select();
            $count = Db::name('goods')->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->count();
            foreach ($list as $key => $value) {
                //查询库存（入库）
                $rk = Db::name('stock_record_good')->alias('a')->join('stock_record b', 'a.pid = b.id')->where('a.good_id', $value['id'])->where('b.status', 1)->where('a.o_type', 0)->where(['a.is_del' => 0, 'a.enterprise_id' => $this->EnterpriseId])->select();
                $rk_num = 0;
                $rk_price = 0;
                $rk_jine = 0;
                foreach ($rk as $k => $v) {
                    $rk_num += $v['number'];
                    $rk_price += $v['price'];
                    $rk_jine += $v['jine'];
                }
                $list[$key]['rk_num'] = $rk_num;
                $list[$key]['rk_price'] = $rk_jine != 0 ? $rk_price / count($rk) : 0;
                $list[$key]['rk_jine'] = $rk_jine;

                //查询库存（出库）
                $ck = Db::name('stock_record_good')->alias('a')->join('stock_record b', 'a.pid = b.id')->where('a.good_id', $value['id'])->where('b.status', 1)->where('a.o_type', 1)->where(['a.is_del' => 0, 'a.enterprise_id' => $this->EnterpriseId])->select();
                $ck_num = 0;
                $ck_price = 0;
                $ck_jine = 0;
                foreach ($ck as $k => $v) {
                    $ck_num += $v['number'];
                    $ck_price += $v['price'];
                    $ck_jine += $v['jine'];
                }
                $list[$key]['ck_num'] = $ck_num;
                $list[$key]['ck_price'] = $ck_price;
                $list[$key]['ck_jine'] = $ck_jine;

                //结存库存
                $list[$key]['jc_num'] = $rk_num - $ck_num;
                $list[$key]['jc_price'] = $rk_jine != 0 ? $rk_price / count($rk) : 0;
                $list[$key]['jc_jine'] = ($rk_jine != 0 && $list[$key]['jc_num'] != 0) ? $rk_jine - ($rk_jine / ($rk_num - $ck_num)) : 0;


                //详细数据
                $list[$key]['goods_category_name'] = Db::name('goods_category')->where('id', $value['id'])->value('name');
            }

            // halt($goods);
            return $this->grid_data($list, $count, $page, $limit);
        }

        return $this->fetch();
    }


    public function add()
    {
        $data = $this->request->param();

        return $this->fetch();
    }
}
